﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Text;
using System.Collections;

namespace VSM
{
    public partial class ServiceManagement : System.Web.UI.Page
    {
        private static DataTable dtContents;
        private static GeneralFunctions GF = null;
        private static string dbType;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                          || Session[Utils.AppConstants.Password] == null
                          || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("../../Login.aspx");
            }
            
            if (!IsPostBack)
            {
                DateTime firstDate = DateTime.Today.AddDays(1 - DateTime.Today.Day);
                DateTime firstDateNextMonth = firstDate.AddMonths(1);
                DateTime lastDate = firstDateNextMonth.AddDays(-1);

                txtFromDate.Text = firstDate.ToString("dd/MM/yyyy");
                txt_ToDate.Text = lastDate.ToString("dd/MM/yyyy");

                dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                loadAdviser();
                
                if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 0)
                {

                }

            }
        }

        private void loadAdviser()
        {
            Services sv = new Services();
            string sql = "select '-1' as Code, 'All' as Name, '' as U_Position, '' as U_DealerCode ";
            sql += " union all ";
            sql += " select Code, Name, U_Position, U_DealerCode from [@TECHNICIAN] where U_DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "'";

            DataSet dsAdviser = sv.ExecuteQuery("VSM", sql);
            DataRow drAll = dsAdviser.Tables[0].NewRow();
            ddl_Adviser.DataSource = dsAdviser.Tables[0];
            ddl_Adviser.DataTextField = "Name";
            ddl_Adviser.DataValueField = "Code";
            ddl_Adviser.DataBind();
        }
        private void loadService() {

            Services sv = new Services();

            string sql = " select ROW_NUMBER() over (ORDER BY gs.GeneralID) RowNum, gs.GeneralID, gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, jk.Name as JobKind, pm.Name as PaymentMethod, ";
            sql += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
            sql += " gs.VehicleCode, gs.VIN, gs.CustomerCode, ";
            sql += " adv.Name AdviserName, tec.Name TechnicianName, ";
            sql += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
            sql += " gs.Odometer, gs.ServiceDate, gs.JobKind, ";
            sql += " gs.VehicleName, gs.Brand, gs.EngineNo, gs.RegNo, gs.Model, gs.ColorCode, gs.CustomerName, gs.OtherName, ";
            sql += " gs.Address, gs.Tel, gs.TaxCode, gs.ContactPerson, gs.ContactPhone ";
            sql += " from U_GeneralService gs ";
            sql += " left join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind";
            sql += " left join VSM_ORG..[@PAYMENTMETHODSVC] pm on pm.Code = gs.Payment ";
            sql += " left join VSM_ORG..[@TECHNICIAN] adv on gs.AdviserCode = adv.Code ";
            sql += " left join VSM_ORG..[@TECHNICIAN] tec on gs.TechnicianCode = tec.Code ";
            //sql += " where gs.GeneralID = " + GeneralID.ToString();

            DateTime fromdate = Utils.AppConstants.ConvertToDate(txtFromDate.Text.Trim());
            DateTime todate = Utils.AppConstants.ConvertToDate(txt_ToDate.Text.Trim());

            sql += " where gs.JobKind <> '004' AND gs.DealerCode = '" + Session[Utils.AppConstants.ID_Dealer] + "'";
            sql += " AND gs.ServiceDate between '" + fromdate.ToString("MM-dd-yyyy") + "' AND '" + todate.ToString("MM-dd-yyyy") + "'";

            string adviserCode = ddl_Adviser.SelectedValue.ToString();
            if (adviserCode != "-1") {
                sql += " AND gs.AdviserCode = '" + adviserCode + "'"; 
            }
            dtContents = sv.ExecuteQuery("DEALER", sql).Tables[0];
            grvPO.DataSource = dtContents;
            grvPO.DataBind();
        }

        protected void btn_Filter_Click(object sender, EventArgs e)
        {
            loadService();
        }
    }
}